//
//  StarViewController.m
//  SVSN1
//
//  Created by Kalyan Mudumbai on 8/27/10.
//  Copyright 2010 GOLDMAN SACHS. All rights reserved.
//

#import "StarViewController.h"
#import "CustomCell.h"

@implementation StarViewController

@synthesize starPickerView;
@synthesize slokaTableView;
@synthesize stars; 
@synthesize slokas;

- (void)viewDidLoad {
    [super viewDidLoad];
	
	//load the slokas
	NSBundle * bundle = [NSBundle mainBundle];
	NSString * plistPath = [bundle pathForResource:@"Telugu-Full" ofType:@"plist"];
	NSLog(@"plist path is: %@", plistPath);
	NSArray * svnSlokas = [[[NSDictionary alloc] initWithContentsOfFile:plistPath] objectForKey:@"Sri Vishnu Sahasrana:mam"];
	slokas = [[NSMutableArray alloc] init];
	for (id oneObject in svnSlokas) {
		if([oneObject isKindOfClass: [NSDictionary class]]) {
			NSDictionary * nsd = (NSDictionary*) oneObject;
			if([[nsd objectForKey:@"id"] intValue] > 49) {
				NSLog(@"Adding %@",nsd);
				[slokas addObject:oneObject];
			}
		}
	}
		
	// load the stars
	self.stars = [[NSArray alloc] initWithObjects:@"Aswini", @"Bharani",@"Kruthika", nil];	
}

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
	return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.starPickerView = nil;
	self.stars = nil;
	self.slokaTableView = nil;
	self.slokas = nil;
}


- (void)dealloc {
	[starPickerView release];
	[slokaTableView release];
	[stars release];
	[slokas release];
    [super dealloc];
}

#pragma mark - 
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
	return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
	return [stars count];
}

#pragma mark - 
#pragma mark Picker Delegate Methods

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
	return [stars objectAtIndex:row];
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
	return [slokas count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
		 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString * customCellIdentifier = @"CustomCellIdentifier";
	CustomCell * cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:customCellIdentifier];
	if(cell == nil) {
		NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
		for (id oneObject in nib){
			if ([oneObject isKindOfClass: [CustomCell class]]) {
				cell = (CustomCell * ) oneObject;
			}
		}
	}
	NSDictionary * slokaForCell = [slokas objectAtIndex:[indexPath row]];
	cell.firstLine.font = [UIFont fontWithName:@"TeluguSangamMN" size:16];
	cell.secondLine.font = [UIFont fontWithName:@"TeluguSangamMN" size:16];
	cell.firstLine.text = [slokaForCell objectForKey:@"f"];
	cell.secondLine.text = [slokaForCell objectForKey:@"s"];
	
	return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
	return kTableViewRowHeight;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
	NSLog(@"Coloring cell %d",[indexPath row]);
    if (indexPath.row == 0 || indexPath.row%2 == 0) {
        UIColor *altCellColor = [UIColor colorWithWhite:0.7 alpha:0.1];
		//UIColor *altCellColor = [UIColor lightGrayColor];
		CustomCell * cc = (CustomCell*)cell;
		cc.cView.alpha = 1.0;
		cc.cView.backgroundColor = altCellColor;
    } else {
		UIColor * altCellColor = [UIColor whiteColor];
		CustomCell * cc = (CustomCell*)cell;
		cc.cView.alpha = 1.0;
		cc.cView.backgroundColor = altCellColor;
	}
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
	   inComponent:(NSInteger)component {
	NSLog(@"Picker row selected is %d",row);
	NSUInteger rowToBeSelected = row * 4;
	NSIndexPath * indexPath = [NSIndexPath indexPathForRow:rowToBeSelected inSection:0];
	[slokaTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

@end
